package org.gwt.grin.client.features;

import java.util.Map;

import org.gwt.grin.client.Feature;
import org.gwt.grin.client.util.Graphics;
import org.gwt.grin.client.util.Rectangle;

import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.event.dom.client.ErrorEvent;
import com.google.gwt.event.dom.client.ErrorHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Image extends Box implements LoadHandler, ErrorHandler {

	protected String url;

	private com.google.gwt.user.client.ui.Image widget;
	private boolean setupMode;
	private boolean imageSetup;

	public Image(String name) {
		super(name);
	}

	@Override
	protected Feature createClone(Map<Feature, Feature> clones) {
		if (!isSetup() || isActivated) {
			throw new IllegalStateException();
		}
		Image result = new Image(null);
		result.show = show; // FIXME
		result.setup(x, y, width, height, outlineWidth, outlineColor,
				fillColor, bgImage, null);
		if (scaledBounds != null) {
			result.scaledBounds = new Rectangle(scaledBounds);
		}
		return result;
	}

	@Override
	protected void initializeClone(Feature original,
			Map<Feature, Feature> clones) {
		super.initializeClone(original, clones);
		Image originalImg = (Image) original;
		setupMode = true;
		setUrl(originalImg.getUrl());
		if (!widget.isAttached()) {
			Style widgetStyle = widget.getElement().getStyle();
			widgetStyle.setVisibility(Visibility.HIDDEN);
			RootPanel.get().add(widget, Short.MIN_VALUE, Short.MIN_VALUE);
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		assert url != null;
		if (url.equals(this.url)) {
			return;
		}
		this.imageSetup = false;
		this.url = url;
		if (setupMode) {
			widget.setUrl(url);
		}
		markAsChanged();
	}

	@Override
	protected Widget createWidget() {
		widget = new com.google.gwt.user.client.ui.Image();
		widget.addLoadHandler(this);
		widget.addErrorHandler(this);
		return widget;
	}

	@Override
	protected int setSetupMode(boolean mode) {
		setupMode = mode;
		super.setSetupMode(mode);
		if (setupMode) {
			widget.setUrl(url);
			if (!widget.isAttached()) {
				Style widgetStyle = widget.getElement().getStyle();
				widgetStyle.setVisibility(Visibility.HIDDEN);
				RootPanel.get().add(widget, Short.MIN_VALUE, Short.MIN_VALUE);
			}
			return imageSetup ? 0 : 1;
		} else {
			imageSetup = false;
			return 0;
		}
	}

	@Override
	public boolean needsMoreSetup() {
		return setupMode && !imageSetup;
	}

	@Override
	public void paintFrame(Graphics gc) {
		if (isChanged()) {
			RootPanel.get().remove(widget);
			Style widgetStyle = widget.getElement().getStyle();
			widgetStyle.setVisibility(Visibility.VISIBLE);
		}
		super.paintFrame(gc);
	}

	@Override
	public void onLoad(LoadEvent event) {
		imageSetup = true;
		sendFeatureSetup();
	}

	@Override
	public void onError(ErrorEvent event) {
		imageSetup = true;
		sendFeatureSetup();
	}

}
